unit CtAdbDlg;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, Buttons;

type
  TFormAddDBTbls = class(TForm)
    Label1: TLabel;
    CBDatabases: TComboBox;
    LBTables: TListBox;
    Label2: TLabel;
    BOk: TBitBtn;
    BCancel: TBitBtn;
    procedure BOkClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CBDatabasesChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure LBTablesDblClick(Sender: TObject);
  private
    { Private declarations }
    FSelected : TStrings;
    function GetDBName: TFileName;
    procedure SetDBName( const fname: TFileName );
  public
    { Public declarations }
    function Execute: boolean;
    function Tables: TStrings;
    property DatabaseName: TFileName read GetDBName write SetDBName;
  end;

implementation

{$R *.DFM}

uses
  DB, DBTables;

procedure AgregarTablasDatabase( const a: string; lt: TStrings );
var
  lista : TStringList;
  i : integer;
begin
  lista := TStringList.Create;
  Session.GetTableNames(a,'*.db',false,false,lista);
  for i := 0 to lista.Count-1 do lt.Add(lista.Strings[i]+'.DB');
  Session.GetTableNames(a,'*.dbf',false,false,lista);
  for i := 0 to lista.Count-1 do lt.Add(lista.Strings[i]+'.DBF');
  lista.Free;
end;

procedure TFormAddDBTbls.BOkClick(Sender: TObject);
begin
  // TODO -cMM: TFormAddDBTbls.BOkClick default body inserted
end;

function TFormAddDBTbls.GetDBName: TFileName;
begin
  Result:= CBDatabases.Text;
end;

procedure TFormAddDBTbls.SetDBName( const fname: TFileName );
var
  p : integer;
begin
  with CBDatabases do
  begin
    if fname > '' then
    begin
      p := Items.IndexOf(fname);
      if p < 0 then p := 0;
    end
    else p := 0;

    if p < Items.Count then ItemIndex := p;
  end;
end;

function TFormAddDBTbls.Execute: boolean;
begin
  result := ShowModal=mrOk;
end;

function TFormAddDBTbls.Tables: TStrings;
var
  i : integer;
begin
  with LBTables, Items do
  begin
    FSelected.Clear;
    for i := 0 to Count-1 do
      if Selected[i] then FSelected.Add(Strings[i]);
  end;
  result := FSelected;
end;

procedure TFormAddDBTbls.FormShow(Sender: TObject);
begin
  if CBDatabases.Items.Count>0 then
  begin
    LBTables.Enabled := true;
    {CBDatabases.ItemIndex := 0;}
    CBDatabasesChange(Self);
  end;
end;

procedure TFormAddDBTbls.CBDatabasesChange(Sender: TObject);
begin
  LBTables.Clear;
  with CBDatabases do
    AgregarTablasDatabase(Text{Items[ItemIndex]},LBTables.Items);
end;

procedure TFormAddDBTbls.FormCreate(Sender: TObject);
begin
  FSelected := TStringList.Create;
  Session.GetDatabaseNames(CBDatabases.Items);
end;

procedure TFormAddDBTbls.FormDestroy(Sender: TObject);
begin
  FSelected.Free;
end;

procedure TFormAddDBTbls.LBTablesDblClick(Sender: TObject);
begin
  ModalResult := mrOk;
end;

end.
